<?php

namespace app\service;

use app\model\OrderHistories;
use app\model\OrderPayments;
use app\model\Orders;
use app\model\PayRecord;
use think\facade\Db;

class PayService
{
    public function paySuccess($params)
    {
        // 处理支付成功逻辑
        $orderPayRecord = PayRecord::where('pay_sn',$params['out_trade_no'])->where('status',0)->find();
        if (!$orderPayRecord){
            throw new \Exception('订单不存在');
        }
        $payementList = OrderPayments::where('transaction_id',$params['out_trade_no'])->select()->toArray();
        if (empty($payementList)){
            throw new \Exception('订单不存在');
        }
        try{
            // 事务
            Db::startTrans();
            // 订单支付记录
            PayRecord::where('pay_sn',$params['out_trade_no'])->update([
                'status'=>1,
                'pay_time'=>date('Y-m-d H:i:s',strtotime($params['success_time'])),
                'channel_no'=>$params['transaction_id'],
                'notify_time'=>date('Y-m-d H:i:s'),
                'updated_at'=>date('Y-m-d H:i:s')
            ]);
            // 订单支付详情
            OrderPayments::where('transaction_id',$params['out_trade_no'])->update([
                'callback'=>json_encode($params),
                'updated_at'=>date('Y-m-d H:i:s')
            ]);
            Orders::whereIn('id',array_column($payementList,'order_id'))->update([
                'pay_status'=>1,
                'pay_time'=>date('Y-m-d H:i:s',strtotime($params['success_time'])),
                'status'=>'paid'
            ]);
            //订单状态历史
            foreach($payementList as $v) {
                OrderHistories::insert([
                    'order_id'=>$v['order_id'],
                    'status'=>'paid',
                    'notify'=>0,
                    'comment'=>'支付成功',
                    'created_at'=>date('Y-m-d H:i:s'),
                ]);
            }
            Db::commit();
        }catch (\Exception $e){
            throw new \Exception($e->getMessage());
        }
    }

    public function payFail($params)
    {
        // 处理支付失败逻辑
        $orderPayRecord = PayRecord::where('pay_sn',$params['out_trade_no'])->where('status',0)->find();
        if (!$orderPayRecord){
            throw new \Exception('订单不存在');
        }
        $payementList = OrderPayments::where('transaction_id',$params['out_trade_no'])->select()->toArray();
        if (empty($payementList)){
            throw new \Exception('订单不存在');
        }
        try{
            // 事务
            Db::startTrans();
            // 订单支付记录
            PayRecord::where('pay_sn',$params['out_trade_no'])->update([
                'status'=>2,
                'notify_time'=>date('Y-m-d H:i:s'),
                'updated_at'=>date('Y-m-d H:i:s')
            ]);
            // 订单支付详情
            OrderPayments::where('transaction_id',$params['out_trade_no'])->update([
                'callback'=>json_encode($params),
                'updated_at'=>date('Y-m-d H:i:s')
            ]);
            Db::commit();
        }catch (\Exception $e){
            throw new \Exception($e->getMessage());
        }
    }
}
